package com.xtgg.history;

import java.util.ArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;
import com.xtgg.R;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class SortActivity extends BaseActivity implements OnClickListener{
	MyAdapter mAdapter;
	ArrayList<JSONObject> mArray = new ArrayList<JSONObject>();

	int mRowNum = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sort);
		
		((TextView) findViewById(R.id.center)).setText("足迹");

		((Button) findViewById(R.id.right)).setText("分享");
		((Button) findViewById(R.id.right)).setTextColor(Color
				.parseColor("#EE7C1C"));
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		
		final PullToRefreshListView ptrlvHeadlineNews = (PullToRefreshListView) findViewById(R.id.listview);
		ptrlvHeadlineNews
				.setMode(com.handmark.pulltorefresh.library.PullToRefreshBase.Mode.PULL_FROM_END);
		ptrlvHeadlineNews
				.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						sendHttpRequest();
					}
				});

		ListView listview = ptrlvHeadlineNews.getRefreshableView();
		mAdapter = new MyAdapter();
		listview.setAdapter(mAdapter);
		listview.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if(((Button)findViewById(R.id.left_btn)).isSelected())
				{
					JSONObject item = mArray.get(arg2- 1);
					
					Intent intent = new Intent(SortActivity.this,MapLocationActivity.class);
					intent.putExtra("addr", item.getString("place"));
					intent.putExtra("lat", item.getDoubleValue("latitude"));
					intent.putExtra("lng", item.getDoubleValue("longitude"));
					startActivity(intent);
				}
			}
		});
		
		((Button)findViewById(R.id.left_btn)).setSelected(true);
		sendHttpRequest();
		
		findViewById(R.id.left_btn).setOnClickListener(this);
		findViewById(R.id.right_btn).setOnClickListener(this);
		findViewById(R.id.btn0).setOnClickListener(this);
		findViewById(R.id.btn1).setOnClickListener(this);
		findViewById(R.id.btn2).setOnClickListener(this);
		findViewById(R.id.left).setOnClickListener(this);
	}
	
	class MyAdapter extends BaseAdapter {
		public int getCount() {
			return mArray.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			FoodHolder foodHold = null;

			if (convertView == null) {
				LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				foodHold = new FoodHolder();

				convertView = mInflater.inflate(R.layout.item_sort, null);
				foodHold.status = (TextView) convertView
						.findViewById(R.id.status);
				foodHold.number = (TextView) convertView.findViewById(R.id.number);
				foodHold.addr = (TextView) convertView
						.findViewById(R.id.addr);

				convertView.setTag(foodHold);
			} else {
				foodHold = (FoodHolder) convertView.getTag();
			}

			JSONObject item = mArray.get(position);
			foodHold.status.setText(String.valueOf(position + 1));
			foodHold.number.setText(item.getString("usernumber") + "人" + item.getString("number") + "个足迹");
			
			if(((Button)findViewById(R.id.left_btn)).isSelected())
				foodHold.number.setText(item.getString("usernumber") + "人" + item.getString("number") + "个足迹");
			else 
				foodHold.number.setText(item.getString("number") + "个足迹");
			
			if(((Button)findViewById(R.id.left_btn)).isSelected())
			{
				if(item.getString("alias").length() > 0)
					foodHold.addr.setText(item.getString("alias"));
				else 
					foodHold.addr.setText(item.getString("place"));
			}
			else
			{
				if(item.getString("username").length() > 0)
					foodHold.addr.setText(item.getString("username"));
				else 
					foodHold.addr.setText(item.getString("nickname"));
			}
			
			return convertView;
		}
	}


	public final class FoodHolder {
		public TextView status;
		public TextView addr;
		public TextView number;
	}

	public void reloadData() {
		mRowNum = 0;
		mArray.removeAll(mArray);
		sendHttpRequest();
	}

	private void sendHttpRequest() {
		boolean show = true;
		if (mArray.size() > 0)
			show = false;

		MgqDataHandler loginHandler = new MgqDataHandler(this, show, false) {
			@Override
			public void onSuccess(String response) {
				Resp ruser = JSON.parseObject(response, Resp.class);
				if (ruser.getState().equals("1")) {
					mArray.addAll(ruser.data);
					mAdapter.notifyDataSetChanged();
					mRowNum = mArray.get(mArray.size() - 1).getIntValue(
							"rownum");
				}
				else if (ruser.getState().equals("2")) 
				{showExitDialog();} 
				else {
				}

				((PullToRefreshListView) findViewById(R.id.listview))
						.onRefreshComplete();
			}
		};

		RequestParams params = new RequestParams();
		if(((Button)findViewById(R.id.left_btn)).isSelected()) params.put("act", "hotFootMark");
		else params.put("act", "loveFootMark");
		params.put("rownum", "" + mRowNum);
		MgqRestClient.post(Constants.INFOMGT, params, loginHandler);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;

		case R.id.left_btn:
			((Button)findViewById(R.id.left_btn)).setSelected(true);
			((Button)findViewById(R.id.right_btn)).setSelected(false);
			reloadData();
			break;
			
		case R.id.right_btn:
			((Button)findViewById(R.id.left_btn)).setSelected(false);
			((Button)findViewById(R.id.right_btn)).setSelected(true);
			reloadData();
			break;
			
		case R.id.btn0:
			finish();
			break;
			
		case R.id.btn1:
			startActivity(new Intent(this,HistoryMapActivity.class));
			finish();
			break;

		case R.id.right:
			showQuery();
			break;
			
		case R.id.login:
			mDialog.dismiss();
			break;
			
		case R.id.action0:
		{
			HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
			
			Intent intent = new Intent(this,ShareActivity.class);
			intent.putExtra("type", 0);
			intent.putExtra("content", "小兔乖乖足迹");
			startActivity(intent);
		}
			mDialog.dismiss();
			break;

		case R.id.action1:
		{
			HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
			
			Intent intent = new Intent(this,ShareActivity.class);
			intent.putExtra("type", 1);
			intent.putExtra("content", "小兔乖乖足迹");
			startActivity(intent);
		}
			mDialog.dismiss();
			break;

		case R.id.action2:
		{
			HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
			
			Intent intent = new Intent(this,ShareActivity.class);
			intent.putExtra("type", 2);
			intent.putExtra("content", "小兔乖乖足迹");
			startActivity(intent);
		}
			mDialog.dismiss();
			break;

		case R.id.action3:
		{
			HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
			
			Intent intent = new Intent(this,ShareActivity.class);
			intent.putExtra("type", 3);
			intent.putExtra("content", "小兔乖乖足迹");
			startActivity(intent);
		}
			mDialog.dismiss();
			break;
		}
	}
	
	boolean hasDraw;
	Dialog mDialog = null;
	void showQuery() {
		mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_share);
		mDialog.findViewById(R.id.action0).setOnClickListener(this);
		mDialog.findViewById(R.id.action1).setOnClickListener(this);
		mDialog.findViewById(R.id.action2).setOnClickListener(this);
		mDialog.findViewById(R.id.action3).setOnClickListener(this);
		mDialog.findViewById(R.id.login).setOnClickListener(this);
		mDialog.show();
	}
	
	 public Bitmap convertViewToBitmap(View view) {
		 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		 int width = wm.getDefaultDisplay().getWidth();
		 int height = wm.getDefaultDisplay().getHeight();
		 
		 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
		 view.draw(new Canvas(b));
		 return b;
	 }
}
